package com.baway.movie.model

import com.baway.movie.api.MovieServerApi
import com.baway.movie.entity.MovieTypeEntity
import com.example.common.repo.BaseRepository
import com.example.http.OkHttpClientManager
import com.example.http.RetrofitManager
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject



class MovieRepository : BaseRepository<MovieServerApi>(){

    /**
     * 获取一级分类
     */
    suspend fun getMutilType(): Flow<List<MovieTypeEntity>> {
        return flow {
            var result = apiServer.getMutilType()
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }

    /**
     * 获取二级分类
     */
    suspend fun getMutilTypeById(pid: Int): Flow<List<MovieTypeEntity>>{
        return flow {
            var result = apiServer.getMutilTypeByPid(pid)
            if (result.code == 200){
                emit(result.data)
            }else{
                emit(listOf())
            }
        }
    }

    override fun createApiServer(): MovieServerApi {
        return RetrofitManager.getRetrofit(OkHttpClientManager.createOkHttpClient()).create(MovieServerApi::class.java)
    }


}